#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "ui_Calendar.h"
#include <QDialog>

#include <QHBoxLayout>
#include <QMessageBox>

#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtXml/QDomNode>
#include <QtXml/QDomNodeList>
#include <QFile>

#include <QTableWidgetItem>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_pChat = new Chat();
    m_pIdeaBox = new IdeaBox();
    m_pCalendar = new Calendar();

    ///when user select a new friend,it must change the connection with the new friend
    connect(this,SIGNAL(selectNewFriend(QString,QString,int)),m_pChat,SLOT(processConnectionChange(QString,QString,int)));

    ui->tabWidget->setCurrentIndex(0);

    ///=================init the frend list==================================
    ui->m_twFriendList->setColumnCount(8);
    ui->m_twFriendList->setColumnWidth(0,90);   //nick name
    ui->m_twFriendList->setColumnWidth(1,40);   //sex
    ui->m_twFriendList->setColumnWidth(2,44);   //state
    ui->m_twFriendList->setColumnWidth(3,100);  //address
    ui->m_twFriendList->setColumnWidth(4,100);  //tel
    ui->m_twFriendList->setColumnWidth(5,100);  //email
    ui->m_twFriendList->setColumnWidth(6,100);  //ip
    ui->m_twFriendList->setColumnWidth(7,50);   //port

    QStringList list;
    list<<QObject::tr("Nick Name")<<QObject::tr("Sex")<<QObject::tr("state")
        <<QObject::tr("Address")<<QObject::tr("Tel")<<QObject::tr("Email")
        <<QObject::tr("IP")<<QObject::tr("Port");

    ui->m_twFriendList->setHorizontalHeaderLabels(list);

    ui->m_twFriendList->hideColumn(3); //
    ui->m_twFriendList->hideColumn(4); //
    ui->m_twFriendList->hideColumn(5); //
    ui->m_twFriendList->hideColumn(6); //
    ui->m_twFriendList->hideColumn(7); //
    //=======================================================================

    //============get server ip and port from confi.xml file=======
    QFile file("Configure\\confi.xml");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QString str = file.readAll();
    QDomDocument doc;
    doc.setContent(str,false);
    QDomNodeList List = doc.elementsByTagName("server");

    QString IP = List.at(0).firstChildElement("ip").text();
    int Port = List.at(0).firstChildElement("port").text().toInt();

    m_ServerConnection.connectToHost(QHostAddress(IP),Port);
    connect(&m_ServerConnection,SIGNAL(newData(MSGHeader,QByteArray)),this,SLOT(interpretData(MSGHeader,QByteArray)));
    //=============================================================

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_tabWidget_currentChanged(int index)
{
    QHBoxLayout layout;

    switch (index)
    {
    case 0:
        layout.addWidget(m_pChat);
        ui->m_tbChat->setLayout(&layout);
        break;
    case 1:
        layout.addWidget(m_pIdeaBox);
        ui->m_tbIdeaBox->setLayout(&layout);

        break;
    case 2:
        layout.addWidget(m_pCalendar);
        ui->m_tbCalendar->setLayout(&layout);

        break;
    default:
        layout.addWidget(m_pChat);
        ui->m_tbChat->setLayout(&layout);
        break;
    }

}
void MainWindow::interpretData(MSGHeader header,QByteArray data)
{
    switch (header.target)
    {
    case YES:
    case NO:
    ///response for login
        receiveUserList(header,data);
        //emit dispatchDataToLogin(header,data);
        break;
    case CHAT:
        emit dispatchDataToChat(header,data);
        break;
    case IDEABOX:
        emit dispatchDataToIdeaBox(header,data);
        break;
    case CALENDAR:
        emit dispatchDataToCalendar(header,data);
        break;
    }

    return ;
}
void MainWindow::sendPackage(QByteArray package)
{
    //m_ServerConnection.write(package);
    //m_ServerConnection.flush();
}
///receive the user list from the login module,and add to the widget
void MainWindow::receiveUserList(MSGHeader header, QByteArray data)
{
    QDomDocument userList;
    userList.setContent(data,false);

    QString name;
    QString IP;
    QString Port;
    QString sex("m");
    QString state("online");

    QDomNodeList list = userList.elementsByTagName("User");
    for (int i=0; i<list.count(); i++)
    {
        name = list.at(i).firstChildElement("Name").text();
        IP = list.at(i).firstChildElement("Ip").text();
        Port = list.at(i).firstChildElement("Port").text();

        ui->m_twFriendList->insertRow(i);
        ui->m_twFriendList->setItem(i,0,new QTableWidgetItem(name));
        ui->m_twFriendList->setItem(i,1,new QTableWidgetItem(sex));
        ui->m_twFriendList->setItem(i,2,new QTableWidgetItem(state));
        ui->m_twFriendList->setItem(i,6,new QTableWidgetItem(IP));
        ui->m_twFriendList->setItem(i,7,new QTableWidgetItem(Port));
    }
}
//select a new friend to chat with
void MainWindow::on_m_twFriendList_itemClicked(QTableWidgetItem* item)
{

    //IP = item-
}

void MainWindow::on_m_twFriendList_itemDoubleClicked(QTableWidgetItem* item)
{

}
void MainWindow::processShowGUI()
{
    show();
}

void MainWindow::on_m_twFriendList_cellClicked(int row, int column)
{
    QString IP;
    int port;
    IP = ui->m_twFriendList->item(row,7)->text();
    port = ui->m_twFriendList->item(row,6)->text().toInt();

    emit selectNewFriend("yaoboyuan",IP,port);
}
